package com.cnhis.cloudhealth.clinical.osworks.commonsql.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OsBckMedicationDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.Xml2JsonUtil;
import com.cnhis.cloudhealth.commons.vo.exception.BaseException;

@Service
public class OsBckMedicationBo {
	
	@Resource
	private OsBckMedicationDao osBckMedicationDao;
	
	@Resource
	private OsHooperBo OsHooperBo;
	
	@SuppressWarnings("rawtypes")
	public List getBckMedication(Map<String, Object> map) {
		List list = null;
		try{
			 list = osBckMedicationDao.getBckMedication(map);
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelVo insertnurseVDD1Edit(Map<String, Object> map) {
		Map<String, Object> mapxml = null;
		ModelVo mv = new ModelVo();
/*		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("lDoc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new BaseException("解析XML文档时发生错误，错误号: %d.");
		}*/
		try {
			//List tmpVAT1 = (List) mapxml.get("rxlist");
			List tmpVAT1 = osBckMedicationDao.getTmpVAT1(map);
			if(tmpVAT1.size()<=0){
				//throw new BaseException("没有找到要提交的发药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("提交错误，科室领药药品为空！");
				return mv;
			}
			Map<String, Object> mapList = new HashMap<String, Object>();
			mapList.put("list", tmpVAT1);
			List vaj01List = osBckMedicationDao.getVaj01List(mapList);
			if(vaj01List.size() > 0){
				String smsg = osBckMedicationDao.getSmsg(tmpVAT1);
				if(smsg != null && smsg != ""){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("明细id:" + smsg + "对应费用已经发药，返回刷新数据重新调整！");
					return mv;
				}
			}
			int vdd03 = OsHooperBo.CoreNewIDEx("VDD1","VDD03",1,mv);
			Map<String, Object> parmap = new HashMap<String, Object>();
			parmap.put("list", tmpVAT1);
			parmap.put("vdd03", vdd03);
			parmap.put("bce01", map.get("bce01"));
			parmap.put("bce03", map.get("bce03"));
			parmap.put("bck01", map.get("bck01"));
			int i1 = osBckMedicationDao.updateVDD1(parmap);
			/*if(i1 <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("更新科室领药错误，请重新保存！");
				return mv;
			}*/
			int i2 =osBckMedicationDao.insertVDD1(parmap);	
			/*if(i2 <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("保存科室领药错误，请重新保存！");
				return mv;
			}*/
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		osBckMedicationDao.deleteDropKbmVDD();
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	@SuppressWarnings("rawtypes")
	public List getBckMedicationGroup(Map<String, Object> map) {
		List list = null;
		try{
			 list = osBckMedicationDao.getBckMedicationGroup(map);
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public List getBckMedicationMingXi(Map<String, Object> map) {
		List list = null;
		try{
			 list = osBckMedicationDao.getBckMedicationMingXi(map);
		}catch(Exception e){
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		return list;
	}
}
